﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vJine.Core.Base;
using vJine.Core.ORM;
using vJine.Core.Web;

namespace vJine.Lua.GUI.BLL {
    public class DataProxy : NodeConfig {

        class DataSpec {
            public string name {
                get;
                set;
            }

            public override string ToString() {
                return this.name;
            }
        }
        public DataProxy() {
            this.ui_ignore = true;
            /*01-初始设置*/
            string host = "127.0.0.1"; int port = 80;

            NodeConfig proxyInit = new NodeConfig() { name = "data-proxy-init", alias = "01-初始设置" };
            proxyInit.is_init = true; proxyInit.tab = 4;
            NodeConfig proxyIP = proxyInit.init_config("proxy-ip", "服务器地址", host);
            proxyIP.s = true; proxyIP.p = true;
            NodeConfig proxyPort = proxyInit.init_config("proxy-port", "服务器端口", port);
            proxyPort.s = true; proxyPort.p = true;
            this.Add(proxyInit);
            //
            proxyInit.OnInvoke = (NodeConfig nConfig) => {
                host = nConfig.configs[0].value as string;
                port = int.Parse(nConfig.configs[1].value.ToString());

                return null;
            };

            NodeConfig proxyLoadSpecs = new NodeConfig() {
                name = "data-load-spec", alias = "02-加载规格", configs = new NodeConfig()
            };
            NodeConfig plsItems = proxyLoadSpecs.init_config("Specs", "产品规格", null);
            plsItems.list = new List<ListItem>(); //TODO:空LIST,luaContext报错
            plsItems.list.Add(new ListItem() {
                Value = "", Checked = true
            });
            proxyLoadSpecs.OnInvoke = (NodeConfig nConfig) => {
                nConfig.guiDispatcer.BeginInvoke(new Action(() => {
                    plsItems.list = null;
                    plsItems.list = new List<ListItem>();
                    plsItems.list.Add(new ListItem() {
                        Value = new DataSpec() {
                            name = "Spec-B"
                        }, Checked = false
                    });
                    plsItems.list.Add(new ListItem() {
                        Value = new DataSpec() {
                            name = "Spec-C"
                        }, Checked = false
                    });
                    plsItems.list.Add(new ListItem() {
                        Value = new DataSpec() {
                            name = "Spec-D"
                        }, Checked = false
                    });
                    plsItems.list.Add(new ListItem() {
                        Value = new DataSpec() {
                            name = "Spec-E"
                        }, Checked = true
                    });
                }));
                
                return null;
            };
            this.Add(proxyLoadSpecs);

            /*03-保存数据*/
            NodeConfig proxySaveData = new NodeConfig() { name = "data-proxy-save", alias = "S1-保存数据" };
            NodeConfig nConfigSpecCatalog = proxySaveData.init_config("spec-catalog", "规格类别", "ABC,D,E");
            nConfigSpecCatalog.s = true;
            NodeConfig nConfigTag = proxySaveData.init_config("tag", "数据标记", "N/A");
            nConfigTag.p = true; nConfigTag.s = true;
            NodeConfig nConfigRemark = proxySaveData.init_config("data-remark", "备注", "");
            
            
            
            proxySaveData.OnInvoke = (NodeConfig nConfig) => {
                string spec_catalog = nConfig.configs[0].value as string;
                string data_tag = nConfig.configs[1].value as string;
                string data_remark = nConfig.configs[2].value as string;

                DataProxy.data_save(host, port, data_tag, spec_catalog, nConfig.Parent.configs);

                return null;
            };
            this.Add(proxySaveData);

            ///*05-打印报表*/
            //NodeConfig proxyPrintReport = new NodeConfig() { name = "data-proxy-print", alias = "05-打印报表" };
            //this.Add(proxyPrintReport);
        }

        public static void data_save(string host, int port, string tag, string spec_catalog, NodeConfig data) {
            string sn = "";
            int? channel = null;
            string spec_name = "";

            HttpClient<string> hc = new HttpClient<string>();

            NodeConfig specResult = null;
            string data_post = hc.get<NodeConfig>(data,
                new Class<NodeConfig>.Property("param"), new Class<NodeConfig>.Property("value"),
                (NodeConfig data_i) => {
                    if (data_i == null) {
                        return false;
                    }
                    if (!data_i.data) {
                        return false;
                    }
                    if (string.IsNullOrEmpty(data_i.param)) {
                        return false;
                    }
                    if (data_i.param.ToUpper() == "SN") {
                        sn = data_i.value.ToString();
                        return false;
                    }
                    if (data_i.param.ToUpper() == "CHANNEL") {
                        channel = int.Parse(data_i.value.ToString());
                        return false;
                    }
                    if (data_i.param.ToUpper() == "SPEC") {
                        spec_name = data_i.value.ToString();
                        return false;
                    }
                    if (data_i.param.ToUpper() == "SPEC-RESULT") {
                        specResult = data_i;
                        return false;
                    }
                    //
                    return true;
                });
            //
            if (string.IsNullOrEmpty(sn)) {
                throw new Core.CoreException("[SN]未指定,请确认后重试");
            }
            if (string.IsNullOrEmpty(sn)) {
                throw new Core.CoreException("[SPEC]未指定,请确认后重试");
            }
            if (channel != null) {
                if (channel < 1) {
                    throw new Core.CoreException("[CHANNEL] < 1,请确认后重试");
                }
                hc.set_query("channel", channel.ToString());
            }
            //
            hc.set(host, port, "mes/data-save.mvc");
            hc.set_query("tag", tag);
            hc.set_query("spec_catalog", spec_catalog);
            hc.set_query("spec_name", spec_name);
            hc.set_query("sn", sn);
            //
            hc.set_param("data", data_post);

            string result = hc.Parse();
            if (!string.IsNullOrEmpty(result)) {
                if (specResult != null) {
                    specResult.value = result;
                }
                MessageBox.Show(result, "测试失败", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            } else {
                if (specResult != null) {
                    specResult.value = "";
                }
                MessageBox.Show(result, "测试通过", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
